import React from 'react';
import {Spin, Card, Button, Table, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import routes from './routes';
import {PERMISSION} from './constants';
import {formatAmount, formatPercent} from '../utils';
import {getDetail} from './api';
import {injectIntl} from './intl';
import styles from './style.css';
import {TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT} from '../constants';
import Export from '../common/Export';
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: res.data
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const {formatMessage} = this.props.intl;
        const operations = [];
        operations.push(<Export key={PERMISSION.export} exportRequest={this.props.exportDetail} />);
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.partSaleOrderCode',
                defaultMessage: '参考占用订单编号'
            }),
            dataIndex: 'partSaleOrderCode'
        }, {
            title: formatMessage({
                id: 'detailPanel.column.totalQuantity',
                defaultMessage: '参考占用备件数量'
            }),
            dataIndex: 'totalQuantity',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.totalAmount',
                defaultMessage: '参考占用备件金额'
            }),
            dataIndex: 'totalAmount',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.rateAmount',
                defaultMessage: '参考占用金额满足率'
            }),
            dataIndex: 'rateAmount',
            render: text => formatPercent(text)
        }];
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.detail',
                                defaultMessage: '资金台账详情信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col></Row>}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="id"
                                dataSource={this.state.data}
                                pagination={false}
                                locale={locale}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    exportDetail: PropTypes.func,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {exportDetail} from './actions';

const mapDispatchToProps = (dispatch, props) => ({
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    exportDetail: () => dispatch(exportDetail(props.id)),
});
export default connect(null, mapDispatchToProps)(injectIntl(DetailPanel));
